package android.medicalcloud.com.yiapp.yilian.ui.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.fragment.BaseFragment;
import android.medicalcloud.com.yiapp.base.net.lisener.ViewNetCallBack;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.yilian.configer.enums.HttpConfig;
import android.medicalcloud.com.yiapp.yilian.logic.ScheduleController;
import android.medicalcloud.com.yiapp.yilian.model.SchRefersModel;
import android.medicalcloud.com.yiapp.yilian.model.ScheduleModeal;
import android.medicalcloud.com.yiapp.yilian.ui.view.adapter.CalderAdapter;
import android.medicalcloud.com.yiapp.yilian.utils.android.GsonTool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.roomorama.caldroid.CaldroidFragment;
import com.roomorama.caldroid.CaldroidListener;
import com.roomorama.caldroid.CalendarHelper;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import hirondelle.date4j.DateTime;

/**
 * Created by wanggenlin on 17-5-18.
 */
public class DateFragment extends BaseFragment  implements CaldroidListener,ViewNetCallBack{
    DateReiver reiver;
    public static  final  String DATE_CLICK_ACTION="date.click.action";
    int currentYear;
    int currentMonth;
    CalderFragment caldroidFragment;
    TextView topBt,bottomBt,toastTv;


    public void setUpHaveTime(){
        topBt.setText("已设置上午有时间");
        topBt.setTextColor(Color.parseColor("#ffffff"));
        topBt.setBackgroundResource(R.drawable.date_set_selected_bg);
    }

    public void setUpHaveDefalutTime(){
        topBt.setText("设置上午有时间");
        topBt.setTextColor(Color.parseColor("#29ab91"));
        topBt.setBackgroundResource(R.drawable.date_set_default_bg);
    }



    public void setDownHaveTime(){
        bottomBt.setText("已设置下午有时间");
        bottomBt.setTextColor(Color.parseColor("#ffffff"));
        bottomBt.setBackgroundResource(R.drawable.date_set_selected_bg);
    }

    public void setDownHaveDefalutTime(){
        bottomBt.setText("设置下午有时间");
        bottomBt.setTextColor(Color.parseColor("#29ab91"));
        bottomBt.setBackgroundResource(R.drawable.date_set_default_bg);
    }


    DateTime today ;


    @Override
    protected void initView(View view) {
        Bundle args = new Bundle();
        Calendar cal = Calendar.getInstance();
        args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
        args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
        args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
        args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, false);
        caldroidFragment = new CalderFragment();
        caldroidFragment.setArguments(args);
        caldroidFragment.setCaldroidListener(this);
        final FragmentTransaction t = getChildFragmentManager().beginTransaction();
        t.replace(R.id.calendar1, caldroidFragment);
        t.commit();
        topBt  =  (TextView)view.findViewById(R.id.topBt);
        bottomBt  =  (TextView)view.findViewById(R.id.bottomBt);
        toastTv  =  (TextView)view.findViewById(R.id.toastTv);
        today = CalendarHelper.convertDateToDateTime(new Date());
        toastTv.setText("请设置"+today.getMonth()+"月"+today.getDay()+"日"+"空闲的时间");
        topBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                CalderAdapter adapter = (CalderAdapter)caldroidFragment.getCurrentAdapter(currentSelectYear,currentMonth);
                if (currentSelectMoth!=adapter.getMonth()){
                    return;
                }
                if (topBt.getText().toString().contains("已")){
                    setUpHaveDefalutTime();
                    HashMap<Integer,Integer> hashMap = adapter.getTimeHas();
                    int type=hashMap.get(currentSelectDay);
                    if (type==1){
                        hashMap.put(currentSelectDay,3);
                    }
                    if (type==0){
                        hashMap.put(currentSelectDay,2);
                    }
                    String  date = currentSelectYear+"-"
                            +(currentSelectMoth<10?(0+""+currentSelectMoth):currentSelectMoth)
                            +"-"+(currentSelectDay<10?(0+""+currentSelectDay):currentSelectDay);
                    type=hashMap.get(currentSelectDay);
                    Logger.e("type====nidaye======="+type);

                    int staues =1;
                    if (type==1||type==3){
                        staues=2;
                    }
                    ScheduleController.getInstance().save(DateFragment.this,date,2,staues);
                    adapter.notifyDataSetChanged();
                    return;
                }
                setUpHaveTime();
                if (adapter!=null){
                    HashMap<Integer,Integer> hashMap = adapter.getTimeHas();
                    if (hashMap.containsKey(currentSelectDay)){
                        int type=hashMap.get(currentSelectDay);
                        Logger.e("-----------aaa--------------"+type);


                        if (type==2){
                            hashMap.put(currentSelectDay,0);
                        }

                        if (type==0){
                            hashMap.put(currentSelectDay,2);
                        }
                        if (type==1){
                            hashMap.put(currentSelectDay,3);
                        }

                        if (type==3){
                            hashMap.put(currentSelectDay,1);
                        }

                    }else{
                        Logger.e("-----------bb--------------");

                        hashMap.put(currentSelectDay,1);
                    }
                    adapter.setTime(hashMap);
                    int type = hashMap.get(currentSelectDay);
                    String  date = currentSelectYear+"-"
                            +(currentSelectMoth<10?(0+""+currentSelectMoth):currentSelectMoth)
                            +"-"+(currentSelectDay<10?(0+""+currentSelectDay):currentSelectDay);
                    ScheduleController.getInstance().save(DateFragment.this,date,1,type==1?2:1);
                }
            }
        });
        bottomBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CalderAdapter adapter = (CalderAdapter)caldroidFragment.getCurrentAdapter(currentSelectYear,currentMonth);
                if (currentSelectMoth!=adapter.getMonth()){
                    return;
                }
                if (bottomBt.getText().toString().contains("已")){
                    setDownHaveDefalutTime();
                    HashMap<Integer,Integer> hashMap = adapter.getTimeHas();
                    int type=hashMap.get(currentSelectDay);
                    if (type==2){
                        hashMap.put(currentSelectDay,3);
                    }
                    if (type==0){
                        hashMap.put(currentSelectDay,1);
                    }
                    String  date = currentSelectYear+"-"
                            +(currentSelectMoth<10?(0+""+currentSelectMoth):currentSelectMoth)
                            +"-"+(currentSelectDay<10?(0+""+currentSelectDay):currentSelectDay);

                     type=hashMap.get(currentSelectDay);
                    Logger.e("type====nidaye======="+type);

                    int staues =1;
                    if (type==2||type==3){
                        staues=2;
                    }

                    ScheduleController.getInstance().save(DateFragment.this,date,staues,2);
                    adapter.notifyDataSetChanged();
                    return;
                }


                setDownHaveTime();
                if (adapter!=null){
                    HashMap<Integer,Integer> hashMap = adapter.getTimeHas();
                    if (hashMap.containsKey(currentSelectDay)){
                        int type=hashMap.get(currentSelectDay);
                        if (type==0){
                            hashMap.put(currentSelectDay,1);
                        }

                        if (type==1){
                            hashMap.put(currentSelectDay,0);
                        }

                        if (type==2){
                            hashMap.put(currentSelectDay,3);
                        }


                        if (type==3){
                            hashMap.put(currentSelectDay,2);
                        }


                    }else{
                        hashMap.put(currentSelectDay,2);
                    }
                    adapter.setTime(hashMap);
                    String  date = currentSelectYear+"-"+(currentSelectMoth<10?(0+""+currentSelectMoth):currentSelectMoth)+"-"+(currentSelectDay<10?(0+""+currentSelectDay):currentSelectDay);
                    int type = hashMap.get(currentSelectDay);

                    ScheduleController.getInstance().save(DateFragment.this,date,type==2?2:1,1);
                }
            }
        });
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(DATE_CLICK_ACTION);
        reiver = new DateReiver();
        getActivity().registerReceiver(reiver,filter);
         today = CalendarHelper.convertDateToDateTime(new Date());
         currentSelectMoth =today.getMonth();
         currentSelectYear =today.getYear();
         currentSelectDay =today.getDay();

        handler.sendEmptyMessageDelayed(0,1000);
    }
    Handler handler = new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            CalderAdapter adapter = (CalderAdapter)caldroidFragment.getCurrentAdapter(today.getYear(),today.getMonth());
            if (adapter!=null){
                HashMap<Integer,Integer> hm =  adapter.getTimeHas();
                if (hm.containsKey(today.getDay())){
                    int type = hm.get(today.getDay());
                    if (type==0){
                        setUpHaveTime();
                        setDownHaveTime();
                        return;
                    }
                    if (type==1){
                        setUpHaveTime();
                        setDownHaveDefalutTime();
                        return;
                    }
                    if (type==2){
                        setUpHaveDefalutTime();
                        setDownHaveTime();
                        return;
                    }
                }else {
                    setUpHaveDefalutTime();
                    setDownHaveDefalutTime();
                }
            }

        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.fra_date;
    }

    public static DateFragment newInstance() {
        DateFragment homeFragment = new DateFragment();
        return homeFragment;
    }

    @Override
    public void onSelectDate(Date date, View view) {

    }



    int currentSelectMoth ;
    int currentSelectYear ;
    int currentSelectDay ;

    public class DateReiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            int day = intent.getIntExtra("day",0);

            int m = intent.getIntExtra("m",0);
            int y = intent.getIntExtra("y",0);

            currentSelectDay = day;
            currentSelectMoth = m;
            currentSelectYear = y;
            toastTv.setText("请设置"+m+"月"+day+"日"+"空闲的时间");
            CalderAdapter adapter = (CalderAdapter)caldroidFragment.getCurrentAdapter(y,m);
            HashMap<Integer,Integer> hm =  adapter.getTimeHas();
            if (hm.containsKey(day)){
                int type = hm.get(day);
                if (type==0){
                    setUpHaveTime();
                    setDownHaveTime();
                    return;
                }
                if (type==1){
                    setUpHaveTime();
                    setDownHaveDefalutTime();
                    return;
                }
                if (type==2){
                    setUpHaveDefalutTime();
                    setDownHaveTime();
                    return;
                }
            }else {
                setUpHaveDefalutTime();
                setDownHaveDefalutTime();
            }

            Logger.e("------------------------");

        }
    }

    @Override
    public void onLongClickDate(Date date, View view) {

    }

    @Override
    public void onChangeMonth(int month, int year) {
        Logger.e("------------------------"+year+"year"+month);
        String  date = year+"-"+(month<10?(0+""+month):month);
        ScheduleController.getInstance().list(new ScheduleRespone(month,year),date);
        ScheduleController.getInstance().refer(new ScheduleRespone(month,year),date);
        currentYear = year;
        currentMonth = month;
    }

    @Override
    public void onCaldroidViewCreated() {

    }




    public class ScheduleRespone implements ViewNetCallBack{
        int month;
        int year;

        public ScheduleRespone(int month, int year) {
            this.month = month;
            this.year = year;
        }

        @Override
        public void onConnectStart(Object o) {

        }

        @Override
        public void onConnectEnd() {

        }

        @Override
        public void onFail(Exception e, Object o, String reslut) {

        }

        @Override
        public void onData(Serializable result, int flag, boolean fromNet, Object o) {
            CalderAdapter adapter = (CalderAdapter)caldroidFragment.getCurrentAdapter(year,month);

            if (flag== HttpConfig.scheduleList.getType()){
                try {
                    ScheduleModeal modeal = GsonTool.jsonToEntity(o.toString(),ScheduleModeal.class);
                    Logger.e("------00000000---------"+adapter.getMonth());
                    adapter.orderStaues(modeal.getCompleteInt(),modeal.getNoCompleteInt());
                }catch (Exception e){

                }
            }
            if (flag== HttpConfig.scheduleRefer.getType()){
                try {
                    Logger.e("------111111---------"+o.toString());
                    SchRefersModel schRefersModel =  GsonTool.jsonToEntity(o.toString(), SchRefersModel.class);

                    adapter.setTime(schRefersModel.getHasPlan());
                }catch (Exception e){

                }
            }

        }
    }

    @Override
    public void onConnectStart(Object o) {

    }

    @Override
    public void onConnectEnd() {

    }

    @Override
    public void onFail(Exception e, Object o, String reslut) {

    }

    @Override
    public void onData(Serializable result, int flag, boolean fromNet, Object o) {

    }
}
